package com.sport.controller;

import com.google.gson.Gson;
import com.sport.constant.WsResultConstant;
import com.sport.context.BaseContext;
import com.sport.pojo.entity.UandR;
import com.sport.pojo.vo.CampMessageVo;
import com.sport.pojo.vo.RoomVo;
import com.sport.pojo.vo.WsResult;
import com.sport.result.Result;
import com.sport.service.RoomService2;
import com.sport.websocket.RoomWs2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.sport.constant.MessageConstant.*;
import static com.sport.constant.MessageConstant.ROOM_DESTROY_ERROR;

@RestController
@RequestMapping("/user/room2")
@Api(tags = "排位房间相关接口")
public class RoomController2 {
    @Autowired
    private RoomService2 roomService;
    @Autowired
    private RoomWs2 roomWs;

    @ApiOperation("用户进入房间")
    @PostMapping("/intoroom")
    public Result uerIntoRoom(@RequestBody UandR uandR) {

        int info = roomService.userIntoRoom(uandR);

        if (info == 1) {
            roomWs.broadcast(uandR.getRoomId(), new Gson().toJson(new WsResult(WsResultConstant.ADD_PEOPLE, "新增人员", new Gson().toJson(uandR))));
            return Result.success(String.valueOf(uandR.getCamp()));
        } else if(info == 2) {
            roomWs.broadcast(uandR.getRoomId(), new Gson().toJson(new WsResult(WsResultConstant.ROOM_FULL, "房间人满", new Gson().toJson(uandR))));
            return Result.success("房间人满",501);
        }else {
            return Result.error(ROOM_INTO_ERROR);
        }
    }
    @ApiOperation("用户更换阵营")
    @PostMapping("/changecamp")
    public Result userChangeCamp(@RequestBody UandR uandR) {

        int info = roomService.userChangeCamp(uandR);
        if (info != 0) {
            roomWs.broadcast(uandR.getRoomId(), new Gson().toJson(new WsResult(WsResultConstant.CHANGE_CAMP, "更换阵营", new Gson().toJson(uandR))));
            return Result.success(new Gson().toJson(uandR));
        } else {
            return Result.error(USER_CHANGERCAMP_ERROR);
        }
    }

    @ApiOperation("用户退出房间")
    @DeleteMapping("/exitRoom")
    public Result exitRoom(@RequestBody UandR uandR) {
        int info = roomService.exitRoom(uandR);
        if (info != 0) {
            roomWs.broadcast(uandR.getRoomId(), new Gson().toJson(new WsResult(WsResultConstant.DELETE_PEOPLE, "退出房间", new Gson().toJson(uandR))));
            return Result.success();
        } else {
            return Result.error(ROOM_DELETE_ERROR);
        }
    }

    @ApiOperation("查找房间")
    @GetMapping("/find")
    public Result findRoomByUserId(String userId) {
        RoomVo roomVo = roomService.findRoomByUserId(userId);
        Gson gson = new Gson();
        String s = gson.toJson(roomVo);
        if (roomVo != null) {
            return Result.success(s);
        } else {
            return Result.error(ROOM_NOT_FOUND);
        }
    }

    @ApiOperation("查询房间人员及阵容信息")
    @GetMapping("/findAllNumber")
    public Result fineAllNumber(String roomId) {
        CampMessageVo campMessageVo = roomService.showRoomNumber(roomId);
        Gson gson = new Gson();
        String s = gson.toJson(campMessageVo);
        if (s != null) {
            return Result.success(s);
        } else {
            return Result.error(FIND_ROOMMESSAGE_ERROR);
        }
    }

    @ApiOperation("摧毁房间")
    @DeleteMapping("/destroyRoom")
    public Result destroyRoom(@RequestBody UandR uandR) {
        if (roomService.destroyRoom(uandR) == 1) {
            roomWs.broadcast(uandR.getRoomId(), new Gson().toJson(new WsResult(WsResultConstant.DESTROY_ROOM, "房间已解散", new Gson().toJson(uandR))));
            return Result.RoomDestroy(ROOM_DESTROY_SUCCESS, "");
        } else {
            return Result.error(ROOM_DESTROY_ERROR);
        }
    }

    @ApiOperation("房主踢人")
    @DeleteMapping("/kickUser")
    public Result kickUser(@RequestBody UandR uandR){
        if (roomService.kickUser(uandR) !=0) {
            roomWs.broadcastKickUser(uandR.getRoomId(),uandR.getUserId(),BaseContext.getCurrentId(), new Gson().toJson(new WsResult(WsResultConstant.KICK_USER, uandR.getUserName()+"被剔除", new Gson().toJson(uandR))));
            return Result.success();
        } else {
            return Result.error("踢出失败");
        }
    }
}
